/*
 * @Date: 2021-09-10 09:03:07
 * @Author: Acckno1
 * @LastEditTime: 2021-09-10 09:24:04
 * @Description: 
 */
#include <iostream>
#include <cstring>
#include <algorithm>
#include <cstdio>
#include <cmath>
#include <vector>
#include <list>
#include <queue>
#include <stack>
#include <map>
#include <unordered_map>
#include <unordered_set>
#include <set>
#include <bitset>
#include <utility>
using namespace std;

#define mm(a, n) memset(a, n, sizeof a)
#define mk(a, b) make_pair(a, b)

const double eps = 1e-6;
const int INF = 0x3f3f3f3f;

typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int, int> PII;
typedef pair<double, double> PDD;
typedef pair<LL, LL> PLL;
typedef pair<int, LL> PIL;

inline void quickread() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
}

const int N = 1010;

int v[N];
int n, m;

void post_order(int u) {
    if (u >= n) return;
    post_order(u * 2 + 1);
    post_order(u * 2 + 2);
    printf("%d%s", v[u], u == 0 ? "\n" : " ");
}

inline void solution() {
    cin >> m >> n;
    while(m -- ) {
        for (int i = 0; i < n; i ++ ) cin >> v[i];
        int flag = v[0] > v[1] ? 1 : -1;
        for (int i = 0; i <= (n - 1) / 2; i ++ ) {
            int left = i * 2 + 1, right = i * 2 + 2;
            if (flag == 1 && (v[i] < v[left] || (right < n && v[i] < v[right]))) flag = 0;
            if (flag == -1 && (v[i] > v[left] || (right < n && v[i] > v[right]))) flag = 0;             
        }
        if (flag == 0) printf("Not Heap\n");
        else printf("%s Heap\n", flag == 1 ? "Max" : "Min");
        post_order(0);
    }
}

int main() {
    freopen("input.txt", "r", stdin);
    quickread();
    solution();
    return 0;
}